package cn.xo68.boot.groovyscript.common;

/**
 * 脚本构建者
 * @author wuxie
 * @date 2018-12-18
 */
public class ScriptBuilder {

    public static ScriptBuilder create(){
        return new ScriptBuilder();
    }

    private final String lineSeparator;
    private final StringBuilder scriptContainer;

    public ScriptBuilder() {
        this(new StringBuilder());
    }

    public ScriptBuilder(StringBuilder scriptContainer) {
        lineSeparator = java.security.AccessController.doPrivileged(
                new sun.security.action.GetPropertyAction("line.separator"));
        this.scriptContainer = scriptContainer;
    }

    public ScriptBuilder appendLine(String lineScript){
        scriptContainer.append(lineScript);
        appendSpaceLine();
        return this;
    }
    public ScriptBuilder append(String lineScript){
        scriptContainer.append(lineScript);
        return this;
    }
    public ScriptBuilder appendSpaceLine(){
        scriptContainer.append(lineSeparator);
        return this;
    }

    public String build(){
        return toString();
    }

    @Override
    public String toString() {
        return scriptContainer.toString();
    }
}
